package cn.edu.ccut.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

public class RefactorXMLParse {

	protected static final String SEPARATOR = File.separator;

	public static String getKeyValue(IFile saturnFile) {
		File file = saturnFile.getLocation().toFile();
		String key = "";

		try {
			
			if (file.toString().endsWith("jsp")) {
				IPath allPath = saturnFile.getFullPath();
				String allPathStr = allPath.toOSString();
				key = allPathStr.replace("\\", "/");
				key = key.replace("/src", "");
			}else{
				
					Document doc = SaturnW3CUtil.getDocument(file.getAbsolutePath());
					
					if (doc != null) {
					Node root = doc.getDocumentElement();
					if (file.toString().endsWith("logic")
							|| file.toString().endsWith("action")
							|| file.toString().endsWith("wf")) {
						Node InformationNode = SaturnW3CUtil.getFirstChildByName(
								root, "information");
						key = SaturnW3CUtil
								.getChildNodeValue(InformationNode, "key");
	
					} else if (file.toString().endsWith("component")) {
	
						Node compontentNode =SaturnW3CUtil.getFirstChildByName(root, "component");
						key = SaturnW3CUtil.getNodeAttributeValue(compontentNode, "key");
					} else if (file.toString().endsWith("saturndata")) {
						String keyName;
						String keyNameSpace;
						Node dataNode = SaturnW3CUtil.getFirstChildByName(root, "data");
						keyNameSpace = SaturnW3CUtil.getNodeAttributeValue(dataNode, "namespace");
						keyName = SaturnW3CUtil.getNodeAttributeValue(dataNode, "name");
						key = keyNameSpace+ "::" + keyName;
					} else if (file.toString().endsWith("validate")) {
						
						Node validateNode = SaturnW3CUtil.getFirstChildByName(root, "validate");
						key = SaturnW3CUtil.getChildNodeValue(validateNode, "key");
					}  
				}
			}

			
		} catch (Exception e) {
		}
		return key;
	}
//	
	public static void renameSelfName(IFile saturnFile, String name, int flag) {
		
		String projectName = saturnFile.getProject().getName();
		String newName = name;
		
		if (name.indexOf(".") != -1) {
			newName = name.substring(0, name.lastIndexOf("."));
		}
		File file = new File(saturnFile.getLocation().toOSString());
		try {
			Document doc = SaturnW3CUtil.getDocument(file.getAbsolutePath());
			if (file.getName().endsWith("saturndata")){
				if (flag == 0) {
					Node dataNode = SaturnW3CUtil.getFirstChildByName(doc.getDocumentElement(), "data");
					SaturnW3CUtil.createAttribute(dataNode, "name", newName);
					
				}else{
					Node dataNode = SaturnW3CUtil.getFirstChildByName(doc.getDocumentElement(), "data");
					SaturnW3CUtil.getNodeAttributeValue(dataNode, "namespace");
					String oldnamespace = SaturnW3CUtil.getNodeAttributeValue(dataNode, "namespace");
					String prefix = oldnamespace.substring(0, oldnamespace
							.indexOf(projectName)
							+ projectName.length() + 1);
					SaturnW3CUtil.createAttribute(dataNode, "namespace", prefix + newName);
				}
			}
			try {
				OutputStream out = new FileOutputStream(file);
				Writer wr = new OutputStreamWriter(out, "UTF-8");
				SaturnW3CUtil.writeDocument(doc, file.getPath());
				wr.close();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
				
		} catch (XMLException e) {
			e.printStackTrace();
		}
	}
}
